#pragma once

#include <Windows.h>
#include <assert.h>
#include <stdio.h>
#include "Buff.h"

#define BLACK   0
#define RED     1
#define LEFT    0
#define RIGHT   1

struct XRBNode
{
    const char*     pszName;
    BOOL            bColor;
    XRBNode*        pChild[2];
    XRBNode*        pParent;

    XRBNode()
    {
        pszName     = "";
        bColor      = 0;
        pChild[0]   = this;
        pChild[1]   = this;
        pParent     = this;
    }
};

typedef struct XRBNode* RBTree;

extern RBTree RBNil;

RBTree  RBSearch(RBTree pTree, const char* pszName);
BOOL    RBInsert(RBTree& pTree, const char* pszName); // invalid
BOOL    RBInsert(RBTree& pTree, RBTree pNode);
BOOL    RBInsert(RBTree& pTree, const char* pszName, XBuff* pBuff);
void    RBDelete(RBTree& pTree, RBTree pNode);

void    RBRotate(RBTree& pTree, RBTree pNode, BOOL bRight);
RBTree  RBInit(RBTree pNode, const char* pszName);
void    RBFixColor(RBTree& pTree, RBTree pNode);
void    RBDelFixColor(RBTree& pTree, RBTree pNode);
RBTree  RBMin(RBTree pNode);
void    RBPrint(RBTree root);
